<template>
  <div class="main">
    <center>
      <el-table
        v-loading="loading"
        :data="
          tableData.filter(
            data =>
              !search ||
              data.car_number.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 80%"
        border
        height="600"
      >
        <el-table-column label="序号" width="180" align="center">
          <template slot-scope="scope">
            <span v-text="getIndex(scope.$index)"></span>
          </template>
        </el-table-column>
        <el-table-column label="车牌号" prop="car_number"> </el-table-column>
        <el-table-column label="Etc区域" prop="area_name"> </el-table-column>
        <el-table-column label="Etc名称" prop="etcname"> </el-table-column>
        <el-table-column label="档案库区域" prop="area_name1">
        </el-table-column>
        <el-table-column label="身份证" prop="idcard"> </el-table-column>
        <el-table-column label="档案库身份证" prop="idcard1"> </el-table-column>
        <el-table-column label="颜色" prop="color"> </el-table-column>
        <el-table-column label="档案库颜色" prop="color1"> </el-table-column>
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入关键字搜索"
            />
          </template>
          <template slot-scope="scope">
            <el-button
              type="success"
              size="mini"
              @click="bukong(scope.$index, scope.row)"
              >布控</el-button
            >
            <!-- 布控时候的弹出层 -->
            <el-dialog
              :visible.sync="dialogFormVisible"
              width="500px"
              top="200px"
            >
              <el-form :model="form">
                <el-form-item label="序号" :label-width="formLabelWidth">
                  <el-input v-model="form.id" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="车牌号" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.car_number"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="身份证号码" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.idcard"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
                <el-form-item label="任务接受用户">
                  <el-select v-model="form.user_id" placeholder="请选择">
                    <el-option
                      v-for="item in option"
                      :key="item.id"
                      :label="item.account"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="描述" :label-width="formLabelWidth">
                  <el-input
                    v-model="form.describe"
                    auto-complete="off"
                  ></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button
                  type="primary"
                  @click="addbukong(scope.$index, scope.row)"
                  >确 定</el-button
                >
              </div>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNumber"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.totalRow"
      >
      </el-pagination>
    </center>
  </div>
</template>

<script>
import { getTaoPaiChe } from "@/api/moxing";
import { getUserlist } from "@/api/moxing";
import { addbukong } from "@/api/moxing";
export default {
  data() {
    return {
      tableData: [],
      search: "",
      pageNumber: 1, //当前页
      pageSize: 10, //每页大小
      totalRow: 0, //总条数
      totalPage: 0, //总页数
      input: "",
      loading: true,

      dialogFormVisible: false, //编辑弹出层默认为关闭
      form: {
        car_number: "",
        idcard: "",
        id: "",
        user_id: "",
        describe: ""
      },
      formLabelWidth: "120px",

      //任务接受用户选择
      options: [],

      //首页跳转的car_number
      home_carnumber: ""
    };
  },
  methods: {
    //增加
    addUser() {
      var params = {
        account: this.formadd.account,
        username: this.formadd.username,
        password: this.formadd.password,
        role_id: this.formadd.role_id
      };
    },
    //时间格式化
    dataFormat(row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },

    //布控弹出框内容
    bukong(index, row) {
      this.dialogFormVisible = true; //开启弹出层
      this.form.car_number = row.car_number;
      this.form.idcard = row.idcard1;
      this.form.id = row.id;
    },
    addbukong(index, row) {
      var params = {
        car_number: this.form.car_number,
        idcard: this.form.idcard,
        id: this.form.id,
        user_id: this.form.user_id,
        describe: this.form.describe
      };
      addbukong("/moxing/bukong", params).then(res => {
        if (res.code === 200) {
          this.$message("修改成功！");
          this.loading = false;
          this.dialogFormVisible = false;
          location.reload();
        } else {
          this.$message(res.data);
        }
      });
    },
    //分页点击方法
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      };
      this.findAll(params);
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageNumber = val;
      var params = {
        pageNumber: this.pageNumber,
        pageSize: this.pageSize
      };
      this.findAll(params);
    },
    findAll(params) {
      getTaoPaiChe("/moxing/gettaopaicar", params).then(res => {
        if (res.code === 200) {
          this.tableData = res.data.list;
          this.totalRow = res.data.totalRow;
          // console.log(this.tableData.length)
          this.loading = false;
        } else if (res.code === 500) {
          this.$message(res.data);
        }
      });
    },
    //添加表格序号
    getIndex($index) {
      return (this.pageNumber - 1) * this.pageSize + $index + 1;
    }
  },
  //初始化的时候初始化表格大小
  created() {
    //获取路由中的车牌号
    debugger;
    if (this.$route.query.car_number == undefined) {
      this.home_carnumber = "";
    } else {
      this.home_carnumber = this.$route.query.car_number;
    }
    var params = {
      pageNumber: this.pageNumber,
      pageSize: this.pageSize,
      car_number: this.home_carnumber
    };
    this.findAll(params);
    getUserlist("/user/getuserlist").then(res => {
      if (res.code === 200) {
        this.option = res.data;
      } else if (res.code === 500) {
        this.$message(res.data);
      }
    });
  }
};
</script>
<style scoped>
.main {
  width: 100%;
  height: 700px;
  margin-top: 10px;
  border: 1px solid red;
}
</style>
